package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcMyJoinFactoryListEntity;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.entity.JcMyFactoryDataEntity;
import com.sgb.jicai.view.fragment.MyCenterMyJoinFactoryFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import org.greenrobot.eventbus.EventBus;
import java.util.HashMap;
import java.util.Map;

public class MyCenterMyJoinFactoryFraModel extends ViewModel {

    private MyCenterMyJoinFactoryFragment fragment;
    private Map map;
    private int page = 1;
    public boolean hasNext = false;//是否有下一页

    public void getList(String status, String searchWords, boolean isLoadMore) {
        map = new HashMap();
        map.put("limit", 10);
        page = isLoadMore ? page + 1 : 1;
        map.put("page", page);
        map.put("status", "0".equals(status) ? "" : status);//状态[0全部 1集采中 2集采完成 ]
        map.put("goodsName", searchWords);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getMyFactoryCenMyJoinList(map).enqueue(new ICallBack<BaseEntity<JcMyFactoryDataEntity<JcMyJoinFactoryListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<JcMyFactoryDataEntity<JcMyJoinFactoryListEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null)
                    return;
                if (null == data.getData())
                    return;
                if (data.getData().getCurrent() < data.getData().getPages()) {
                    hasNext = true;
                } else {
                    hasNext = false;
                }
                fragment.showList(data.getData().getRecords(), isLoadMore, hasNext);
            }
        });
    }

    public void getH5Url(JcMyJoinFactoryListEntity entity) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("manufacturer").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.showUrl(data.getData().getUrl() + "?id=" + entity.getOrderNo() + "&no=" + entity.getSaleNo());
                } else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    //退出集采
    public void quitJiCai(String orderNo) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().quitFactoryJc(orderNo).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(fragment.getContext(), "退出集采成功");
                    EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_MYCENTER_XUFANG_FAQI_TABNUM, ""));
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });

    }

//我的加盟删除接口调用
    public void deleteJoin(String orderNo) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().productSaleOrderDelete(orderNo).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(fragment.getContext(), "删除信息成功");
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void setActivity(MyCenterMyJoinFactoryFragment myCenterMyJoinFactoryFragment) {
        this.fragment = myCenterMyJoinFactoryFragment;
    }
}
